package com.myapp.wicket;           
import org.apache.wicket.Component;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.protocol.http.WebApplication;

public class Application extends WebApplication {

    public Application() 
    { }

    @Override
    public Class getHomePage() 
    {
        return HomePage.class;
    }
    
    @Override
    public Session newSession(Request request, Response response)
    {
        return new SignInSession(request);
    }
    
    @Override
    protected void init()
    {
        super.init();
        
        getSecuritySettings().setAuthorizationStrategy(new IAuthorizationStrategy()
        {
            @Override
            public <T extends Component> boolean isInstantiationAuthorized(Class<T> componentClass) 
            {
                if(AuthenticatedWebPage.class.isAssignableFrom(componentClass))
                {
                    if(((SignInSession)Session.get()).isSignedIn())
                    {
                        return true;
                    }
                     throw new RestartResponseAtInterceptPageException(SignIn.class);
                }
                return true;
            }

            @Override
            public boolean isActionAuthorized(Component component, Action action) {
                return true;
            }
        });
    }
}
